package com.imooc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ComputeServlet extends HttpServlet {
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		/**
		 * 1.计算n以内的自然数和，然后返回
		 * 2.使用Servlet计算n以内自然数的累加和，并输出，参数n通过url地址传递
		 * 3.提示：在Servlet中获取的参数是字符串类型，
		  *      需要使用Integer.parseInt(String s)方法转换为整型再进行计算。
		 */
		
		String n1 = request.getParameter("n");
		int n = Integer.parseInt(n1);
		int sum = 0;
		for(int i = 0; i<=n; i++) {
			sum = sum + i;
		}
		//System.out.println(n); //测试输出结果
		String html = "<h1 style ='color:red'>"+ sum +"</h1><hr>";
		response.getWriter().println(html); 
		
		
		
	}
}
